package com.mindframe.acelerometro;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {

	final String TAG = getClass().getName();
	TextView tvX, tvY, tvZ;
	Tablero tablero;
	Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this.getApplicationContext();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		tablero = new Tablero(context);

		setContentView(tablero);
		
		// tvX = (TextView) findViewById(R.id.tvX);
		// tvY = (TextView) findViewById(R.id.tvY);
		// tvZ = (TextView) findViewById(R.id.tvZ);

	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	class Tablero extends View implements SensorEventListener {
		final String TAG = getClass().getName();
		Paint paint, pRect;
		Rect rect1, rect2;
		List<Obstaculo> listaObs = new ArrayList<Obstaculo>();
		int screen_height, screen_width;
		Punto p;
		Vibrator vib;
		int fondo;

		public Tablero(Context context) {
			super(context);

			paint = new Paint();
			paint.setColor(Color.GREEN);
			paint.setAntiAlias(true);

			pRect = new Paint();
			pRect.setColor(Color.BLACK);

			fondo = Color.BLUE;

			screen_height = getWindowManager().getDefaultDisplay().getHeight();
			screen_width = getWindowManager().getDefaultDisplay().getWidth();

			p = new Punto();
			vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);

			rect1 = new Rect(100, 50, 200, 100);
			rect2 = new Rect(200, 150, 300, 200);

			Obstaculo ob1 = new Obstaculo(5, 8, rect1);
			Obstaculo ob2 = new Obstaculo(3, 5, rect2);

			listaObs.add(ob1);
			listaObs.add(ob2);

			SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);

			List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);

			if (sensors.size() != 0) {
				Log.d(TAG, "encontrado sensor");

				sm.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_GAME);
			}

		}

		@Override
		public void onDraw(Canvas canvas) {

			canvas.drawColor(fondo);
			for (Obstaculo ob : listaObs) {
				canvas.drawRect(ob.rect, pRect);
			}

			canvas.drawCircle(p.x, p.y, 10, paint);
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}

		@SuppressLint("ParserError")
		@Override
		public void onSensorChanged(SensorEvent event) {

			int speedX = (int) event.values[SensorManager.DATA_X];
			int speedY = (int) event.values[SensorManager.DATA_Y];

			p.x -= 3 * speedX;
			p.y += 3 * speedY;

			if (p.x <= 0)
				p.x = 0;
			if (p.x >= screen_width) {
				p.x = screen_width;

			}
			if (p.y <= 0)
				p.y = 0;
			if (p.y >= screen_height)
				p.y = screen_height;

			animaRect(listaObs);
			fondo = controlColision(listaObs, p);

			invalidate();

		}

		private void animaRect(List<Obstaculo> listaObs) {
			for (Obstaculo ob : listaObs) {

				if (ob.rect.right >= 480) {
					Log.d(TAG, "Choque: RIGHT");
					ob.changeY();
				}
				if (ob.rect.left <= 0) {
					Log.d(TAG, "Choque: LEFT");
					ob.changeY();
				}
				if (ob.rect.bottom >= 800) {
					Log.d(TAG, "Choque: BOTTOM");
					ob.changeX();
				}
				if (ob.rect.top <= 0) {
					Log.d(TAG, "Choque: TOP");
					ob.changeX();
				}

				ob.rect.bottom += ob.speed_x;
				ob.rect.top += ob.speed_x;
				ob.rect.left -= ob.speed_y;
				ob.rect.right -= ob.speed_y;
			}
		}
	}

	public int controlColision(List<Obstaculo> listaObs, Punto p) {

		int color = Color.BLUE;

		for (Obstaculo ob : listaObs) {
			Rect rect = ob.rect;
			int left, right, top, bottom;
			left = rect.left;
			right = rect.right;
			top = rect.top;
			bottom = rect.bottom;

			if (p.x > left && p.x < right && p.y > top && p.y < bottom) {
				color = Color.RED;
				break;
			} else {
				color = Color.BLUE;
			}
		}

		return color;
	}

	class Punto {
		float x;
		float y;
	}

}
